/**
 * Created with File
 * Author: Chuans
 * Github: https://github.com/chuans
 * Date: 2018/9/6
 * Time: 下午4:34
 */
import * as React from 'react';
// import HomeStore from '../home/store';
import {observer} from 'mobx-react';
import UTable from './table';
import SearchHeader from '../../components/search-header'
import {message, Spin} from 'antd';
import CoinStore from './store';
import {searchForm} from './form';


@observer
export default class extends React.Component<any> {
    public props: {
        states: any;
        match: any;
    }

    constructor(props: any) {
        super(props);
    }

    public async componentDidMount() {
        try {
            const result: any = await CoinStore.getList(CoinStore.searchStatus);

            if (result) {
                message.error(result);
            }
        } catch (err) {
            message.error(err);
        }
    }

    public onHandleSubmit = async (values: any) => {
        try {
            const result = await CoinStore.getList(Object.assign(CoinStore.searchStatus, values));

            if (result) {
                message.error(result);
            }
        } catch (err) {
            message.error(err);
        }
    }

    public onPageChange = async ({current}: Common.Pager) => {
        await this.onHandleSubmit({pageIndex: current});
    }

    public render() {
        const {pageSize, pageIndex} = CoinStore.searchStatus;

        const pager: Common.Pager = {
            total: CoinStore.total,
            pageSize,
            current: pageIndex,
        };

        return (
            <Spin spinning={CoinStore.listLoading} size="large">
                <SearchHeader onHandleSubmit={this.onHandleSubmit} dataForm={searchForm}/>
                <UTable
                    datas={CoinStore.list}
                    pager={pager}
                    onPageChange={this.onPageChange}
                />
            </Spin>
        )
    }
}